<?php
namespace app\admin\logic;
use app\admin\validate\Floor as FloorValidate;

class FloorLogic extends BaseLogic {

    /**
     * 获取楼层查询条件
     * @param $branchid
     * @return array
     */
    public function get_search_floor_maps($branchid){
        $maps = [];
        if($branchid != -1){
            $maps[] = ['branchid','=',$branchid];
        }
        return $maps;
    }

    /**
     * 检查新增楼层规则
     * @param $params
     * @return mixed
     */
    public function check_add_floor_rule($params){
        $validate = new FloorValidate();
        $r = $validate->scene('add')->check($params);
        if(!$r){
            return $this->get_response(0,$validate->getError());
        }

        $maps = [];
        $maps[] = ['branchid','=',$params['branchid']];
        $maps[] = ['name','=',trim($params['name'])];
        $f_detail = model('floor')->get_detail($maps,'id');
        if(!empty($f_detail)){
            return $this->get_response(0,'该楼层已存在');
        }
        return $this->get_response(1,'OK');
    }

    /**
     * 检查修改楼层规则
     * @param $params
     * @return mixed
     */
    public function check_save_floor_rule($params){
        $validate = new FloorValidate();
        $r = $validate->scene('edit')->check($params);
        if(!$r){
            return $this->get_response(0,$validate->getError());
        }

        $maps = [];
        $maps[] = ['id','=',$params['floorid']];
        $f_detail = model('floor')->get_detail($maps,'id');
        if(empty($f_detail)){
            return $this->get_response(0,'该楼层不存在');
        }

        $maps = [];
        $maps[] = ['id','<>',$params['floorid']];
        $maps[] = ['branchid','=',$params['branchid']];
        $maps[] = ['name','=',trim($params['name'])];
        $detail = model('floor')->get_detail($maps,'id');
        if(!empty($detail)){
            return $this->get_response(0,'该楼层已存在');
        }

        $data['f_detail'] = $f_detail;
        return $this->get_response(1,'OK',$data);
    }
}